package cn.mothin.frame.core.db;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.mothin.frame.core.bean.SearchData;
import cn.mothin.frame.core.interfaces.vo.IQueryPage;
import cn.mothin.frame.util.SqlUtil;

@SuppressWarnings("serial")
public class QueryPage implements IQueryPage, Serializable {
	// 导出
	protected boolean queryForExport = false;
	// 导出标题列表
	protected String queryForExportContent = null;
	// 页码
	protected int page = 1;
	// 分页长度
	protected int rows = 10;
	// 排序对象
	protected String sort = null;
	// 排序方式
	protected String order = "asc";
	// 总记录数
	protected int total = 0;
	// 结果集
	@SuppressWarnings("rawtypes")
	protected List result = null;
	// 搜索条件
	protected Map<String, SearchData> search;

	// public String Page2Json(String... includeFields) {
	// EasyuiGridData data = new EasyuiGridData();
	// return data.toJson(this, includeFields);
	// }

	public Object getPageJsonObject() {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", total);
		map.put("result", result);
		return map;
	}

	public int getFirst() {
		if ((this.page < 1) || (this.rows < 1)) {
			return -1;
		}
		return (this.page - 1) * this.rows;
	}

	/**
	 * 获取全部的页数
	 * 
	 * @return 页数
	 */
	public int getTotalPages() {
		int count = this.total / this.rows;
		if (this.total % this.rows > 0) {
			count++;
		}
		return count;
	}

	/**
	 * 判断是否有下一页
	 * 
	 * @return true：有；false：没有
	 */
	public boolean isHasNext() {
		return this.page + 1 <= getTotalPages();
	}

	/**
	 * 获取下一页的页码
	 * 
	 * @return 页码
	 */
	public int getNextPage() {
		if (isHasNext()) {
			return this.page + 1;
		}
		return this.page;
	}

	/**
	 * 判断是否有前一页
	 * 
	 * @return true：有；false：没有
	 */
	public boolean isHasPre() {
		return this.page - 1 > 0;
	}

	/**
	 * 获取前一页的页码
	 * 
	 * @return 页码
	 */
	public int getPrePage() {
		if (isHasPre()) {
			return this.page - 1;
		}
		return this.page;
	}

	public boolean isQueryForExport() {
		return queryForExport;
	}

	public void setQueryForExport(boolean queryForExport) {
		this.queryForExport = queryForExport;
	}

	public String getQueryForExportContent() {
		return queryForExportContent;
	}

	public void setQueryForExportContent(String queryForExportContent) {
		this.queryForExportContent = queryForExportContent;
	}

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getRows() {
		return rows;
	}

	public void setRows(int rows) {
		this.rows = rows;
	}

	public String getSort() {
		return sort;
	}

	public void setSort(String sort) {
		this.sort = sort;
	}

	public String getOrder() {
		return order;
	}

	public void setOrder(String order) {
		this.order = order;
	}

	public int getTotal() {
		return total;
	}

	public void setTotal(int total) {
		this.total = total;
	}

	@SuppressWarnings("rawtypes")
	public List getResult() {
		return result;
	}

	@SuppressWarnings("rawtypes")
	public void setResult(List result) {
		this.result = result;
	}

	@Override
	public String modifySQL(String sql) {
		String newSQL = sql;
		if (sort != null && sort.length() > 0 && !sort.contains(";")) {
			newSQL = SqlUtil.removeOrders(newSQL);
			newSQL += " ORDER BY " + sort;
			newSQL += " ";
			newSQL += order.equals(IQueryPage.ASC) ? IQueryPage.ASC
					: IQueryPage.DESC;
		}
		return newSQL;
	}

	public Map<String, SearchData> getSearch() {
		return search;
	}

	public void setSearch(Map<String, SearchData> search) {
		this.search = search;
	}

}
